package com.kfm.base.thread.day04;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Demo01 {

    public static void main(String[] args) {
        BlockingQueue<Integer> blockingQueue = new ArrayBlockingQueue<>(1);

        Runnable odd = () -> {
            for (int i = 1; i <= 10; i++) {
                if (i % 2 != 0){
                    try {
                        blockingQueue.put(i);
                        System.out.println("奇数: " + i);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };

        Runnable even = () -> {
            for (int i = 1; i < 11; i++) {
                if (i % 2 == 0){
                    try {
                        blockingQueue.take();
                        System.out.println("偶数：" + i);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        };

        Thread thread = new Thread(odd);
        Thread thread1 = new Thread(even);

        thread.start();
        thread1.start();
    }
}
